package com.github.aly8246.core.driver

import com.github.aly8246.core.configuration.Configurations.Companion.configuration
import com.github.aly8246.core.util.PrintImpl
import com.github.aly8246.core.util.StringUtils
import com.mongodb.MongoClient
import com.mongodb.MongoCredential
import com.mongodb.client.MongoCollection
import com.mongodb.client.MongoDatabase
import net.sf.jsqlparser.schema.Table
import org.bson.Document
import java.sql.*
import java.util.*
import java.util.concurrent.Executor

class MongoConnection() : Connection {
    private lateinit var mongoClient: MongoClient
    private lateinit var dbAddress: MongoAddress
    private var autoCommit: Boolean = true

    constructor(dbAddress: MongoAddress) : this() {
        //连接mongodb
        if (StringUtils.isNotEmpty(configuration.username) && StringUtils.isEmpty(configuration.password)) {
            val createCredential = MongoCredential.createCredential(configuration.username!!, dbAddress.dbName(), configuration.password!!.toCharArray())
            this.mongoClient = MongoClient(dbAddress, mutableListOf(createCredential))
        } else {
            this.mongoClient = MongoClient(dbAddress)
        }
        this.dbAddress = dbAddress
    }

    override fun prepareStatement(sql: String?): PreparedStatement {
        TODO("not implemented") //To change body of created functions use File | Settings | File Templates.
    }

    fun database(): MongoDatabase {
        return mongoClient.getDatabase(dbAddress.dbName())
    }

    fun getCollection(collectionName: String): MongoCollection<Document> {
        return this.database().getCollection(collectionName)
    }

    fun getCollection(table: Table): MongoCollection<Document> {
        return this.database().getCollection(table.name)
    }

    override fun prepareStatement(sql: String?, resultSetType: Int, resultSetConcurrency: Int): PreparedStatement {
        TODO("not implemented") //To change body of created functions use File | Settings | File Templates.
    }

    override fun prepareStatement(sql: String?, resultSetType: Int, resultSetConcurrency: Int, resultSetHoldability: Int): PreparedStatement {
        TODO("not implemented") //To change body of created functions use File | Settings | File Templates.
    }

    override fun prepareStatement(sql: String?, autoGeneratedKeys: Int): PreparedStatement {
        TODO("not implemented") //To change body of created functions use File | Settings | File Templates.
    }

    override fun prepareStatement(sql: String?, columnIndexes: IntArray?): PreparedStatement {
        TODO("not implemented") //To change body of created functions use File | Settings | File Templates.
    }

    override fun prepareStatement(sql: String?, columnNames: Array<out String>?): PreparedStatement {
        TODO("not implemented") //To change body of created functions use File | Settings | File Templates.
    }

    override fun rollback() {
    }

    override fun rollback(savepoint: Savepoint?) {
    }

    override fun getHoldability(): Int {
        TODO("not implemented") //To change body of created functions use File | Settings | File Templates.
    }

    override fun setNetworkTimeout(executor: Executor?, milliseconds: Int) {
        TODO("not implemented") //To change body of created functions use File | Settings | File Templates.
    }

    override fun commit() {
    }

    override fun <T : Any?> unwrap(iface: Class<T>?): T {
        TODO("not implemented") //To change body of created functions use File | Settings | File Templates.
    }

    override fun setTransactionIsolation(level: Int) {
        TODO("not implemented") //To change body of created functions use File | Settings | File Templates.
    }

    override fun setAutoCommit(autoCommit: Boolean) {
        if (!autoCommit) this.autoCommit = autoCommit
    }

    override fun abort(executor: Executor?) {
        TODO("not implemented") //To change body of created functions use File | Settings | File Templates.
    }

    override fun prepareCall(sql: String?): CallableStatement {
        TODO("not implemented") //To change body of created functions use File | Settings | File Templates.
    }

    override fun prepareCall(sql: String?, resultSetType: Int, resultSetConcurrency: Int): CallableStatement {
        TODO("not implemented") //To change body of created functions use File | Settings | File Templates.
    }

    override fun prepareCall(sql: String?, resultSetType: Int, resultSetConcurrency: Int, resultSetHoldability: Int): CallableStatement {
        TODO("not implemented") //To change body of created functions use File | Settings | File Templates.
    }

    override fun getClientInfo(name: String?): String {
        TODO("not implemented") //To change body of created functions use File | Settings | File Templates.
    }

    override fun getClientInfo(): Properties {
        TODO("not implemented") //To change body of created functions use File | Settings | File Templates.
    }

    override fun getAutoCommit(): Boolean {
        TODO("not implemented") //To change body of created functions use File | Settings | File Templates.
    }

    override fun setCatalog(catalog: String?) {
        TODO("not implemented") //To change body of created functions use File | Settings | File Templates.
    }

    override fun getWarnings(): SQLWarning {
        TODO("not implemented") //To change body of created functions use File | Settings | File Templates.
    }

    override fun getCatalog(): String {
        TODO("not implemented") //To change body of created functions use File | Settings | File Templates.
    }

    override fun setHoldability(holdability: Int) {
        TODO("not implemented") //To change body of created functions use File | Settings | File Templates.
    }

    override fun getSchema(): String {
        TODO("not implemented") //To change body of created functions use File | Settings | File Templates.
    }

    override fun isValid(timeout: Int): Boolean {
        TODO("not implemented") //To change body of created functions use File | Settings | File Templates.
    }

    override fun close() {
        this.mongoClient.close()
        PrintImpl().info("Weekend connection closed")
    }

    override fun isClosed(): Boolean {
        TODO("not implemented") //To change body of created functions use File | Settings | File Templates.
    }

    override fun createNClob(): NClob {
        TODO("not implemented") //To change body of created functions use File | Settings | File Templates.
    }

    override fun createBlob(): Blob {
        TODO("not implemented") //To change body of created functions use File | Settings | File Templates.
    }

    override fun createArrayOf(typeName: String?, elements: Array<out Any>?): java.sql.Array {
        TODO("not implemented") //To change body of created functions use File | Settings | File Templates.
    }

    override fun isWrapperFor(iface: Class<*>?): Boolean {
        TODO("not implemented") //To change body of created functions use File | Settings | File Templates.
    }

    override fun setReadOnly(readOnly: Boolean) {
        TODO("not implemented") //To change body of created functions use File | Settings | File Templates.
    }

    override fun nativeSQL(sql: String?): String {
        TODO("not implemented") //To change body of created functions use File | Settings | File Templates.
    }

    override fun createStruct(typeName: String?, attributes: Array<out Any>?): Struct {
        TODO("not implemented") //To change body of created functions use File | Settings | File Templates.
    }

    override fun setClientInfo(name: String?, value: String?) {
        TODO("not implemented") //To change body of created functions use File | Settings | File Templates.
    }

    override fun setClientInfo(properties: Properties?) {
        TODO("not implemented") //To change body of created functions use File | Settings | File Templates.
    }

    override fun releaseSavepoint(savepoint: Savepoint?) {
        TODO("not implemented") //To change body of created functions use File | Settings | File Templates.
    }

    override fun createClob(): Clob {
        TODO("not implemented") //To change body of created functions use File | Settings | File Templates.
    }

    override fun isReadOnly(): Boolean {
        TODO("not implemented") //To change body of created functions use File | Settings | File Templates.
    }

    override fun createStatement(): Statement {
        return createStatement(0, 0, 0)
    }

    override fun createStatement(resultSetType: Int, resultSetConcurrency: Int): Statement {
        return createStatement(resultSetType, resultSetConcurrency, 0)
    }

    override fun createStatement(resultSetType: Int, resultSetConcurrency: Int, resultSetHoldability: Int): Statement {
        return MongoStatement(this, resultSetType, resultSetConcurrency, resultSetHoldability)
    }

    override fun setSavepoint(): Savepoint {
        TODO("not implemented") //To change body of created functions use File | Settings | File Templates.
    }

    override fun setSavepoint(name: String?): Savepoint {
        TODO("not implemented") //To change body of created functions use File | Settings | File Templates.
    }

    override fun getTypeMap(): MutableMap<String, Class<*>> {
        TODO("not implemented") //To change body of created functions use File | Settings | File Templates.
    }

    override fun clearWarnings() {
        TODO("not implemented") //To change body of created functions use File | Settings | File Templates.
    }

    override fun getMetaData(): DatabaseMetaData {
        TODO("not implemented") //To change body of created functions use File | Settings | File Templates.
    }

    override fun getTransactionIsolation(): Int {
        TODO("not implemented") //To change body of created functions use File | Settings | File Templates.
    }

    override fun setSchema(schema: String?) {
        TODO("not implemented") //To change body of created functions use File | Settings | File Templates.
    }

    override fun getNetworkTimeout(): Int {
        TODO("not implemented") //To change body of created functions use File | Settings | File Templates.
    }

    override fun setTypeMap(map: MutableMap<String, Class<*>>?) {
        TODO("not implemented") //To change body of created functions use File | Settings | File Templates.
    }

    override fun createSQLXML(): SQLXML {
        TODO("not implemented") //To change body of created functions use File | Settings | File Templates.
    }
}